import React, { ReactElement } from 'react'
import styles from './index.less';
import { Form, Input, Button } from 'antd';
import { userLogin } from '@/api/login'
import { history } from 'umi'

interface Props {

}

const onFinish = (values: any) => {
    userLogin(values).then((res: any) => {
        sessionStorage.setItem('token', res.token)
        history.push('/')
    })
};

const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
};

export default function index({ }: Props): ReactElement {
    return (
        <div className={styles.login}>
            <div className={styles.card}>
                <Form
                    name="basic"
                    labelCol={{ span: 8 }}
                    wrapperCol={{ span: 16 }}
                    initialValues={{ remember: true }}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item
                        label="用户名"
                        name="username"
                        rules={[{ required: true, message: '请输入用户名!' }]}
                    >
                        <Input />
                    </Form.Item>

                    <Form.Item
                        label="密码"
                        name="password"
                        rules={[{ required: true, message: '请输入登陆密码!' }]}
                    >
                        <Input.Password />
                    </Form.Item>

                    <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                        <Button type="primary" htmlType="submit">
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </div>

        </div>
    )
}
